<?php

App::uses('Role', 'Model');

class User extends AppModel {
    
    // define model name
    var $name = 'User';
    
    // define default password
    var $pseudoPassword = '12346789';
    
    // define model schema
    var $schema = array(
        'username'          => array('type' => 'string'),
        'password'          => array('type' => 'string'),
        'role_id'           => array('type' => 'int'),
        'full_name'         => array('type' => 'string'),
        'address'           => array('type' => 'string'),
        'phone'             => array('type' => 'string'),
        //'picture_id' => array('type' => 'blob')
        'responsabilities'  => array('type' => 'string'), 
        'active'            => array('type' => 'bool'),
        'created'           => array('type' => 'datetime'),
        'deletedOn'         => array('type' => 'datetime')
    );
    
    // define validation rules
    /*var $validate = array(      
        'username' => array(
            'email'         => array(
                'rule'      => 'email',
                'required'  => true,
                'message'   => 'Please provide a valid email address'
            ),
            'between'       => array(
                'rule'      => array('between', 8, 75),
                'message'   => 'Must have between 8 and 75 characters'
            ),
            'unique'        => array(
                'rule'      => 'isUnique',
                'message'   => 'Email already in use'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Password cannot be empty',true)
            )
    );*/
      
    
    public function hashPassword($password) {
        return Security::hash($password, null, true);
    }
    
//    public function beforeSave() {
//        
//        parent::beforeSave();
//        
//        $valid = true;
//        
////        // password match validation
////        if ($this->data['User']['password'] != $this->hashPassword($this->data['User']['retype_password'])) {
////            $this->invalidate('password','Passwords do not match');
////            $valid = false;
////	}
//        
//        // @todo password complexity validation
//        
//        // pseudo-password elimination
//        
//        
//        return $valid;
//    }
    
    public function getRandomPassword() {
        return '123456';
    }
    
    public function getAvailableRoles(&$scope,$id) {

        $this->availableRoles = array();
    
        $dbRoles = $scope->Role->find('all',array('conditions' => array('hierarchy LIKE' => "%|{$id}|%",'deletedOn' => null)));
        foreach ($dbRoles as $dbRole) {
            $this->availableRoles[$dbRole['Role']['id']] =$dbRole['Role']['name'];
        }
        
        return $this->availableRoles;
    }
}

?>